// ==UserScript==
// @name     Youtube 2 MPV
// @version  1
// @grant    none
// @match    https://www.youtube.com/watch*
// ==/UserScript==

let btn = document.createElement("button");
btn.className = "yt-uix-button yt-uix-button-size-default yt-uix-button-opacity yt-uix-button-has-icon no-icon-markup action-gnome-mpv yt-uix-tooltip";
btn.style = "font-weight: bold;";
btn.title = "GNOME MPV";
btn.setAttribute("data-tooltip-text", "Open in GNOME MPV");

let content = document.createElement("span");
content.className = "yt-uix-button-content";
content.textContent = "GNOME MPV";
btn.appendChild(content);

btn.onclick = function() {
  location.href = "ytmpv://" + location.href;
};

function insert() {
  console.log("GNOME MPV: Activate");
  document.querySelector("#watch8-secondary-actions").appendChild(btn);
};

insert()

let obs = new MutationObserver(function() {
  if (location.href.indexOf("watch") < 0) return;
  for (let i = btn; i; i = i.parentElement) {
    if (i == document.body) return; 
  }
  insert();
});
obs.observe(document.body, {childList: true});
